package sorting;

public class SelectionSort
{
    private int[] myData;
    
    public SelectionSort(int[] data)
    {
        myData = data;
    }
    public static void main(String[] args)
    {
        int[] data = new int[20];
        if (args.length > 20) {
            System.err.println("Not valid for more than 20 entries");
        }
        int i = 0;
        for (String s : args) {
            data[i++] = Integer.parseInt(s);    
        }
        
        SelectionSort sorter = new SelectionSort(data);
        System.out.println("Before sorting: " + sorter);
        sorter.sort();
        System.out.println("After sorting: " + sorter);
        System.out.println(SelectionSort.getNotes());
    }
    
    private void sort()
    {
        for (int i = 0; i < myData.length; i++) {
            int min = i;
            for (int j = i + 1; j < myData.length; j++) {
                if (myData[j] < myData[min]) {
                    min = j;
                }
            }
                
            if (min != i) {
                SortUtils.swap(myData, i, min);
            }
        }
    }
    
    @Override
    public String toString()
    {
        StringBuilder sb = new StringBuilder();
        for (int i : myData) {
            sb.append(i + " ");
        }
        
        return sb.toString();
    }
    
    public static String getNotes()
    {
        return "The worst case complexity is O(n^2)";
    }
}
